<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }

    .header, .footer {
      width: 1200px;
      margin: 0 auto;
      background-color: skyblue;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 50px;
      height: 120px;
      color: #fff;
    }

    .footer {
      height: 300px;
    }

    ul, li {
      list-style: none;
    }

    ul {
      width: 1200px;
      display: flex;
      flex-wrap: wrap;
      margin: 0 auto;
      justify-content: space-between;
      padding-top: 10px;
    }

    li {
      width: 290px;
      border: 1px solid #333;
      margin-bottom: 10px;
      padding: 5px;
      box-sizing: border-box;
    }

    li > img {
      width: 278px;
      height: 300px;
      display: block;
    }

    .pagination {
      width: 1200px;
      margin: 10px auto;
      height: 50px;
      display: flex;
      align-items: center;
    }

    .pagination > .prev,
    .pagination > .next {
      width: 50px;
      height: 30px;
      cursor: pointer;
      background-color: orange;
      font-size: 24px;
      color: #fff;
      display: flex;
      justify-content: center;
      align-items: center;
    }

    .pagination > .disable {
      cursor: not-allowed;
      background-color: #ccc;
    }

    .pagination > .total {
      font-size: 30px;
      font-weight: 700;
      margin: 0 20px;
    }

    .pagination > select {
      font-size: 22px;
      padding-left: 20px;
      margin-left: 30px;
    }
  </style>
  <script src="./dm_list.js"></script>
</head>
<body>

  <div class="header">网页头部</div>
  <div class="pagination">
    <span class="prev disable">&lt;</span>
    <span class="total">1 / 100</span>
    <span class="next">&gt;</span>
    <select>
      <option value="4">4</option>
      <option value="8">8</option>
      <option value="12">12</option>
      <option value="16">16</option>
    </select>
  </div>
  <ul>
  
  </ul>
  <div class="footer">网页底部</div>
  <script>
        var contentbox = document.querySelector('ul');
        var totalbox = document.querySelector('.total');
        var selectEle = document.querySelector('select');
        var prevBtn = document.querySelector('.prev');
        var nextBtn = document.querySelector('.next');
        var current  = 1;  // 当前页码
        var pagesize = 4;  // 每页显示的条数
        var total    = 0; // 总页数
        function bindHTML(){
            var bindList = mylist.slice((current-1)*pagesize, current*pagesize);
            contentbox.innerHTML = bindList.reduce(function(pre, item){
                return pre + `
                <li>
                    <img src="${item.pic}" alt="">
                    <p>${item.name}</p>
                    <p>城市:${item.city}</p>
                    <p>地址:${item.address}</p>
                    <p>价格:${item.price}</p>
                    <p>时间:${item.showTim}</p>
                </li>`
            },'')
            total = Math.ceil(mylist.length / pagesize);
            // console.log(total);
            // console.log(typeof total);

            prevBtn.className =  current === 1 ?'prev disable':'prev';
            // console.log(current);
            // console.log(typeof current);
            nextBtn.className =  current === total?'next disable':'next';

            
            //更新左上角的数据
            totalbox.innerHTML = `${current} / ${total}`;
            // 更新下拉框的数据
            selectEle.value = pagesize;
        }

        bindHTML();


        // 1.点击上一页
        prevBtn.onclick = function(){
            if(current === 1) return;
            current--;
            bindHTML();
        }

        // 2.点击下一页
        nextBtn.onclick = function(){
            if(current === total) return
            current++; // 改变当前页 
            bindHTML();
        }


        // 3.点击下拉框
        selectEle.onchange = function(){
            pagesize = selectEle.value-0; 
            // selectEle.value 选中的内容 非数字 
            // selectEle.value-0 选中的内容 转换成数字
            current = 1;
            bindHTML();
        }

  </script>
</body>
</html>